package com.springbootjsp.controller;

import com.alibaba.fastjson.JSONObject;
import com.springbootjsp.model.User;
import com.springbootjsp.service.impl.TestServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

import static jdk.nashorn.internal.runtime.regexp.joni.Config.log;

@Controller
public class login1 {
    @Autowired
    TestServiceImpl service;

    @GetMapping("/")
    public String index(){
        return "login11";
    }

    @GetMapping("/login11")
    @ResponseBody
    public JSONObject login(String name,String pwd){
        User user = new User(name, pwd);
        List<User> login = service.login(user);
        JSONObject json = new JSONObject();
        json.put("code",9999);
        json.put("msg","登陆失败");
        if(!login.isEmpty()){
            json.put("code",200);
            json.put("msg","登陆成功");
            json.put("data",login);
        }
        return json;
    }

    @GetMapping("/insert")
    public String insert(){
        return "insert";
    }

    @GetMapping("/insert1")
    public JSONObject insert1(String name,String pwd,int status){
        User user =new User(name,pwd,status);
        boolean b = service.insertUser(user);
        JSONObject json = new JSONObject();
        json.put("code",9999);
        json.put("msg","注册失败");
        if(b){
                json.put("code",200);
                json.put("msg","注册成功");
                json.put("data",user);
        }
        return json;
    }
}
